import cache from './cache';
const { userGroup }: any = cache;

export interface UserGroupInfoProp {
  id: string;
  fullGroupId: string;
  groupCode: string;
  groupName: string;
  [propsname: string]: any;
}

/**
 *  缓存用户组信息
 *
 * @param {string} id 用户组id
 * @param {UserGroupInfoProp} data 用户组信息对象
 */
export const setUserGroup = ({ id, data }: { id: string; data: UserGroupInfoProp }) => {
  userGroup.set(id, data);
};

/**
 *  获取缓存的用户组信息的指定字段
 *
 * @param {string} id 用户组id
 * @param {string} field 指定字段名
 */
export const getUserGroupField = ({ id, field = 'groupName' }: { id: string; field: string }) => {
  const roleInfo = userGroup.get(id);
  if (!roleInfo) return '';
  return roleInfo[field] || '';
};

/**
 *  获取缓存的用户组信息
 *
 * @param {string} id 用户组id
 */
export const getUserGroupInfo = (id: string) => {
  const roleInfo = userGroup.get(id);
  if (!roleInfo) return '';
  return roleInfo;
};
